﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using JSONPTest.Model;
using JSONPTest.Models;

namespace JSONPTest.Controllers
{
    /*
    The WebApiConfig class may require additional changes to add a route for this controller. Merge these statements into the Register method of the WebApiConfig class as applicable. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using JSONPTest.Model;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Location>("Locations");
    config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
    */
    public class LocationsController : ODataController
    {
        private LocationContext db = new LocationContext();

        // GET: odata/Locations
        [Queryable]
        public IQueryable<Location> GetLocations()
        {
           List<Location> locs = new List<Location>();
            Location l = new Location();
            l.ID = 1;
            l.LocationName = "Topeka DMV";
            l.CountyID = 89;
            locs.Add(l);

            l = new Location();
            l.ID = 2;
            l.LocationName = "Wichita DMV";
            l.CountyID = 87;
            locs.Add(l);

            l = new Location();
            l.ID = 3;
            l.LocationName = "Johnson County DMV";
            l.CountyID = 46;
            locs.Add(l);

            return locs.AsQueryable();



        }


        [Route("locations/count")]
        [HttpGet]
        public IEnumerable<LocationCount> GetLocationCount()
        {
            List<LocationCount> locs = new List<LocationCount>();
            LocationCount l;
            
            l = new LocationCount();
            l.LocationName = "shawnee";
            l.Count = 2;
            locs.Add(l);

            l = new LocationCount();
            l.LocationName = "johnson";
            l.Count = 3;
            locs.Add(l);

            l = new LocationCount();
            l.LocationName = "allen";
            l.Count = 1;
            locs.Add(l);

            return locs;
        }


        //// GET: odata/Locations(5)
        //[Queryable]
        //public SingleResult<Location> GetLocation([FromODataUri] int key)
        //{
        //    return SingleResult.Create(db.Locations.Where(location => location.ID == key));
        //}

        // PUT: odata/Locations(5)
        public async Task<IHttpActionResult> Put([FromODataUri] int key, Location location)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != location.ID)
            {
                return BadRequest();
            }

            db.Entry(location).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LocationExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(location);
        }

        // POST: odata/Locations
        public async Task<IHttpActionResult> Post(Location location)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Locations.Add(location);
            await db.SaveChangesAsync();

            return Created(location);
        }

        // PATCH: odata/Locations(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Location> patch)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Location location = await db.Locations.FindAsync(key);
            if (location == null)
            {
                return NotFound();
            }

            patch.Patch(location);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LocationExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(location);
        }

        // DELETE: odata/Locations(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            Location location = await db.Locations.FindAsync(key);
            if (location == null)
            {
                return NotFound();
            }

            db.Locations.Remove(location);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool LocationExists(int key)
        {
            return db.Locations.Count(e => e.ID == key) > 0;
        }
    }
}
